// // Copyright (c) 2009 All Right Reserved // // Stephen Toub // stoub@microsoft.com // 2009-01-01 // Contains ... namespace LargoCommon.Midi { using System; using System.Globalization; using System.IO; using System.Text; using Music; /// MIDI event to select an instrument for the channel by assigning a patch number. [Serializable] public sealed class VoiceProgramChange : VoiceEvent { #region Fields /// The category status byte for ProgramChange messages. private const byte CategoryStatusByte = 0xC; /// The number of the program to which to change (0x0 to 0x7F). private byte number; #endregion #region Constructors /// Initializes a new instance of the VoiceProgramChange class. /// The delta-time since the previous message. /// The channel to which to write the message (0 through 15). /// The instrument to which to change. public VoiceProgramChange(long deltaTime, MidiChannel channel, MidiMelodicInstrument number) : this(deltaTime, channel, (byte)number) { } /// Initializes a new instance of the VoiceProgramChange class. /// The delta-time since the previous message. /// The channel to which to write the message (0 through 15). /// The number of the program to which to change. public VoiceProgramChange(long deltaTime, MidiChannel channel, byte number) : base(deltaTime, CategoryStatusByte, channel) { this.Number = number; } #endregion #region Properties /// Gets the number of the program to which to change (0x0 to 0x7F). /// General musical property. public byte Number { get => this.number; private set { if (value > 127) { throw new ArgumentOutOfRangeException(nameof(value), value, "The number must be in the range from 0 to 127."); } this.number = value; } } /// Gets The first parameter as sent in the MIDI message. /// General musical property. public override byte Parameter1 => this.number; /// Gets The second parameter as sent in the MIDI message. /// General musical property. public override byte Parameter2 => 0; #endregion #region To String /// Generate a string representation of the event. /// A string representation of the event. public override string ToString() { var sb = new StringBuilder(); sb.Append(base.ToString()); sb.Append("\t"); if (Enum.IsDefined(typeof(MidiMelodicInstrument), (int)this.number)) { sb.Append((MidiMelodicInstrument)this.number); } else { sb.Append("0x"); sb.Append(this.number.ToString("X2", CultureInfo.CurrentCulture.NumberFormat)); } return sb.ToString(); } #endregion #region Methods /// Write the event to the output stream. /// The stream to which the event should be written. public override void Write(Stream outputStream) { if (outputStream == null) { return; } //// Write out the base event information base.Write(outputStream); //// Write out the data outputStream.WriteByte(this.number); } #endregion } }